import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/afterSales/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询GoodWill预算数据'
});
const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询GoodWill预算数据详情'
});
const getAddDesc = formatMessage({
    id: 'api.getAdd',
    defaultMessage: '新增GoodWill预算数据'
});
const getModifyDesc = formatMessage({
    id: 'api.getModify',
    defaultMessage: '修改GoodWill预算数据'
});
const getAbandonDesc = formatMessage({
    id: 'api.getAbandon',
    defaultMessage: '作废'
});
const getSendDesc = formatMessage({
    id: 'api.getSendDesc',
    defaultMessage: '下发'
});
const getConfirmDesc = formatMessage({
    id: 'api.getConfirmDesc',
    defaultMessage: '确认'
});
const getUploadDesc = formatMessage({
    id: 'api.getUploadDesc',
    defaultMessage: '上传'
});
const goodWillsDesc = formatMessage({
    id: 'api.goodWillsDesc',
    defaultMessage: 'GoodWill查询'
});
const getDealersDesc = formatMessage({
    id: 'api.message.getDealersDesc',
    defaultMessage: '查询总代信息'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/goodwillSettlement/init`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询GoodWill预算数据列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/goodwillSettlements/forCurrentUser?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询GoodWill结算数据详情
export const getDetail = id => request(`${baseUrl}/goodwillSettlements/${id}`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
// 新增
export const newOrder = data => request(`${baseUrl}/goodwillSettlements`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAddDesc));
// 修改
export const modifyOrder = (id, data) => request(`${baseUrl}/goodwillSettlements/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getModifyDesc));

// 作废
export const abandonOrder = (id, rowVersion) => request(`${baseUrl}/goodwillSettlements/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));
// 下发
export const sendOrder = (id, rowVersion) => request(`${baseUrl}/goodwillSettlements/${id}/send`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getSendDesc));

const getPiExcelDesc = formatMessage({
    id: 'api.message.getPiExcelDesc',
    defaultMessage: '导出Pi'
});
//导出Pi
export const getPiExcel = id => request(`${baseUrl}/goodwillSettlements/${id}/exportPiExcel`, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, getPiExcelDesc))
    .then(res => res.data);

const getCoverExcelDesc = formatMessage({
    id: 'api.message.getCoverExcelDesc',
    defaultMessage: '导出封面'
});
//导出封面
export const getCoverExcel = id => request(`${baseUrl}/goodwillSettlements/${id}/exportCoverExcel`, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, getCoverExcelDesc))
    .then(res => res.data);
// 确认
export const confirmOrder = (id, data) => request(`${baseUrl}/goodwillSettlements/${id}/confirm`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getConfirmDesc));
//上传
export const uploadOrder = (id, data) => request(`${baseUrl}/goodwillSettlements/${id}/upload`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getUploadDesc));
//查询goodWill主清单
export const getGoodWillList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/goodWills/forGoodwillSettlement?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, goodWillsDesc));
};
// 快捷查询总代
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forGoodWillBudget/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forGoodWillBudget?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
